package ypmits.components.slider {
	import flash.display.BlendMode;
	import ypmits.components.Component;
	import ypmits.components.ComponentSkin;
	import ypmits.components.slider.event.SliderEvent;
	import ypmits.components.util.TextFieldUtility;
	import ypmits.font.Ronda;
	import ypmits.setup.EasySetup;
	import ypmits.utils.SimpleMath;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Rectangle;

	/**
	 * @author Dave Lenz
	 */
	public class Slider extends Component {

		private var func : Function;
		protected var background : Sprite;
		protected var thumb : Sprite;
		private var ds : DropShadowFilter = new DropShadowFilter( 2, 45, 0, 1, 2, 2, 1, 3, true, false, false );
		private var slidername : TextFieldUtility;
		protected var slidervalue : TextFieldUtility;
		private var thumbrect : Rectangle;

		
		public function Slider( skin : ComponentSkin = null ) {
			!skin ? skin = new ComponentSkin() : null;
			skin.backgroundalpha = .8;
			
			super( skin );
			
			draw( );
			
			setDefaults( );
			
			background.addEventListener( MouseEvent.MOUSE_WHEEL, wheelListener );
			thumb.addEventListener( MouseEvent.MOUSE_WHEEL, wheelListener );
		}

		
		override public function set width(value : Number) : void {
			skin.width = value;
			redraw( );
		}
		
		
		private function draw() : void {
			slidername = new TextFieldUtility( skin.name, new Ronda().fontName, 8, 0xFFFFFF, false, "left", false, 0 );
			slidername.y = -5;
			slidername.kerning = true;
			addChild( slidername );
			
			background = new Sprite( );
			background.addEventListener( MouseEvent.MOUSE_DOWN, backgroundHandler );
			background.filters = [ ds ];
			addChild( background );
			
			thumb = new Sprite( );
			thumb.buttonMode = true;
			thumb.addEventListener( MouseEvent.MOUSE_DOWN, sliderDrag );
			thumb.filters = [ new BevelFilter( 1, 45, 0xFFFFFF, 1, 0, 1, 1, 1, 1, 3 ) ];
			EasySetup.instance.stage.addEventListener( MouseEvent.MOUSE_UP, stopSliderDrag );
			addChild( thumb );
			
			slidervalue = new TextFieldUtility( String( skin.defaultvalue ), new Ronda().fontName, 6, 0xFFFFFF, false, "right", false, 0 );
			slidervalue.y = -1;
			slidervalue.blendMode = BlendMode.INVERT;
			skin.showvalue ? addChild( slidervalue ) : null;

			redraw( );
		}

		
		private function redraw() : void {
			background.graphics.clear();
			background.graphics.beginFill( skin.backgroundcolor, skin.backgroundalpha );
			background.graphics.drawRect( 0, 0, skin.width - slidername.width - 4, 10 );
			background.graphics.endFill( );
			background.x = slidername.x + slidername.width + 4;

			slidervalue.x = background.width - slidervalue.width;// + skin.width - slidervalue.width; // - slidervalue.width;

			thumb.graphics.clear();
			thumb.graphics.beginFill( skin.thumbcolor, 1 );
			thumb.graphics.drawRect( 0, 0, skin.thumbwidth, 10 );
			thumb.graphics.endFill( );
			
			alpha = skin.alpha;

			thumbrect = new Rectangle( background.x, 0, background.width - skin.thumbwidth, 0 );
		}

		
		private function backgroundHandler( e : MouseEvent ) : void {
			thumb.x = mouseX - ( thumb.width * .5 );
			thumb.startDrag( false, thumbrect );
			
			enterHandler();
		}

		
		private function wheelListener( e : MouseEvent ) : void {
			var n : Number = ( thumb.x ) + e.delta;
			if( ( n >= background.x ) && ( n <= 100 + background.x ) ) {
				thumb.x = n;
			}
			
			enterHandler( null );
		}

		
		public function setDefaults() : void {
			var min : Number = skin.minimumvalue;
			var max : Number = skin.maximumvalue;
			var def : Number = skin.defaultvalue;
			var oneperc : Number = (max - min) / 100;
//			var oneperc : Number = (skin.maximumvalue - skin.minimumvalue) / ( background.width - skin.thumbwidth );
			thumb.x = background.x + SimpleMath.roundNumber( ( ( def - min ) / oneperc ), 2 );

			//skin.maximumvalue - skin.minimumvalue
//			thumb.x = SimpleMath.roundNumber( background.x + ( ( (skin.minimumvalue / skin.maximumvalue ) / 10  ) * skin.defaultvalue ), 1 );
			
			enterHandler( );
		}
		
		
		private function sliderDrag( e : Event = null ) : void {
			addEventListener( Event.ENTER_FRAME, enterHandler );
			
			thumb.startDrag( false, thumbrect );
			
			enterHandler();
		}
		
		
		private function stopSliderDrag( e : MouseEvent = null ) : void {
			stopDrag( );
			removeEventListener( Event.ENTER_FRAME, enterHandler );
		}


		public function enterHandler( e : Event = null ) : void {
			var factor : Number = (skin.maximumvalue - skin.minimumvalue) / ( background.width - skin.thumbwidth );
			
			var evt : SliderEvent = new SliderEvent( SliderEvent.CHANGE );
			if( skin.roundvalue ) {
				evt.value = Math.round( ( thumb.x * factor ) + skin.minimumvalue );
			} else {
				evt.value =  ( thumb.x * factor ) + skin.minimumvalue;
			}
			slidervalue.text = String( SimpleMath.roundNumber( evt.value, 1 ) );
			evt.slider = this;
			
			if( skin.func != null ) skin.func( evt );
		}

		
		override public function dispose() : void {
			removeEventListener( Event.ENTER_FRAME, enterHandler );
		}
	}
}
